#!/usr/bin/env python3
# -*- coding: utf-8 -*

import os
import sys 

sys.path.append("..")
from package.utility import ensure_exists

def split_line2words(logline):
    """split timer log's data to json format"""
    words = logline.split()
    assert len(words)==4, f"unexpected line: {logline}"
    level_id = len(words[0])
    return (level_id, words[1], words[3])

def checktagduplicate(path):
    """Check tag for duplicates"""
    # timerlog, clean_cache = args
    # timerlog = os.path.abspath(timerlog)
    # logdir = os.path.dirname(timerlog)
    # basename = os.path.basename(timerlog) #basename返回文件名
    # output = os.path.join(logdir, basename.replace(".log", ".json"))

    # # remove output if clean_cache is set as True.
    # if clean_cache is True and os.path.exists(output):
    #     os.remove(output)

    # if os.path.exists(output):
    #     return
    logdir = os.path.dirname(path)
    ensure_exists(logdir)
    taglist = []
    with open(path, "r", encoding="utf-8") as fp:
        loglines = fp.readlines()
    for logline in loglines:
        level_id, keywords, val = split_line2words(logline.strip())
        if keywords not in taglist:
            taglist.append(keywords)
        else:
            print(f"keyword: {keywords} is duplicated.")

def main():
    path="/home/mrz/code/JXPAMG_branch/bottleneck/example/reapet0/pid0.log"
    checktagduplicate(path)

if __name__ == "__main__":
    main()
